package com.wyvoid.forum.web.action.admin;

import com.wyvoid.forum.domain.Settings;
import com.wyvoid.forum.service.ISettingsService;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * User: cjp
 * Date: 2007-7-14
 * Time: 20:06:05
 */
@UrlBinding("/admin/setting.do")
public class SettingAction extends BaseAdminAction {
    @SpringBean
    private ISettingsService settingsService;
    private HashMap<String, String> settingsMap;

    public void setSettingsService(ISettingsService settingsService) {
        this.settingsService = settingsService;
    }

    public HashMap<String, String> getSettingsMap() {
        return settingsMap;
    }

    public void setSettingsMap(HashMap<String, String> settingsMap) {
        this.settingsMap = settingsMap;
    }

    @DefaultHandler
    public Resolution settingForm() {
        settingsMap=new HashMap<String,String>();
        List<Settings> settingsList = settingsService.getSettings();
        for (Settings settings : settingsList) {
            settingsMap.put(settings.getVariable(), settings.getValue());
        }
        return getAdminResolution("settingForm");
    }

    public Resolution setting() {
        Set<String> keySet=settingsMap.keySet();
        for(String key:keySet){
            Settings settings=new Settings(key,settingsMap.get(key));
            settingsService.updateSettings(settings);
        }

        return getAdminResolution("suc");
    }
}
